// pages/server_end/task/task.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    listnav: ['任务大厅', '已接任务'],
    nindex: 0,
    currentPage: 0,
    currentSize: 10,
    totalPage: 1,
    list:[],
    noOrder:1,
    orderId:0,
    keyword:'',
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.getOrder();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getMessage();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.hasMoreData) {
      this.getMessage();
   } 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  chooseNav(e) {
    this.setData({
        nindex:e.currentTarget.dataset.index,
        page:1
    })
    this.getOrder();
  },
  doSearch(){
    this.setData({
      page:1
    })
    this.getOrder();
  },
  getOrder: function() {
    var that = this;
    let param = {
        keyword:that.data.keyword,
        type: that.data.nindex,
        page:that.data.page,
        token:that.data.token
    }
    wx.showNavigationBarLoading();  //在当前页面显示导航条加载动画
    request.request(url.missionlist, param, url.REQUEST_METHOD_GET).then((res) => {
          wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
          if (that.data.page == 1) {
              that.setData({
                list:[],
                noOrder:1,
              })
          }
          var contentlistTem = that.data.list;
            if (res.data.data.data.length > 0) {
              var contentlist = res.data.data.data;
              if (contentlist.length < 10) {
                that.setData({
                  list: contentlistTem.concat(contentlist),
                  hasMoreData: false,
                  noOrder:0,
                })
              } else {
                that.setData({
                  list: contentlistTem.concat(contentlist),
                  hasMoreData: true,
                  page: that.data.page + 1,
                  noOrder:0,
                })
              }
            }else{
               that.setData({
                hasMoreData: false
               })
            }
    })

  },
  goDetail(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'../order/order?id='+id
    })
  },
  gotoorder(e)
  {
	   var id = e.currentTarget.dataset.id;
	  wx.navigateTo({
	    url: '../order/order?id='+id,
	  })
  },
  graborder(id){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    wx.showLoading({title: '请求中'});
    request.request(url.graorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.hideLoading();
      wx.showToast({
        title: res.data.msg,
        icon:'none'
      })
      if(res.data.code==1){
        setTimeout(function(){
          that.setData({
            page:1
          })
          that.getOrder();
          wx.navigateTo({
            url: '../order/order?id='+id,
          })
        },1500)
       }
    })  	   
  },
  subStart(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      orderId:id
    })
    wx.requestSubscribeMessage({
      tmplIds: [
        '4Lrgf9Bnb2GS_vPaq-x-o0tefhfyUDZU7h6vG4y4x6Q',
        '8AunnNXQefxgIuTYEgv5tcx3uvVrMJeNBkJp7CSUtAc',
      ],
        success:(res)=> {
          that.graborder(that.data.orderId);							
        },
        fail:(res)=> {
          that.graborder(that.data.orderId);								
        },
      })		
    //that.graborder(that.orderId);	
  },
})